﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using Common;

public partial class website_admin_insurance_orderlist : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        switch (RequestBase.GetFormString("otype"))
        {
            case "loadingdata":
                LoadingData();
                break;
            case "deleteitems":
                Delete();
                break;
        }

        if (!IsPostBack)
        { 
            ProductDataBind();
        }
    }

    #region 类别列表数据绑定 
    void ProductDataBind()
    {
        DataTable dt = BLL.CommonBLL.Select("InsuranceProductBase", "*", " AND ProductNumber LIKE '00007%' ORDER BY LineNum ASC");
        DropdownlistDataBind(1, "00000000-0000-0000-0000-000000000000", dt);
    }
    /// <summary>
    /// 类别列表邦定
    /// </summary>
    /// <param name="intLayer">级别</param>
    /// <param name="parentId">父目录ID</param>
    /// <param name="dt">目录数据表</param>
    void DropdownlistDataBind(int intLayer, string parentId, DataTable dt)
    {
        if (dt == null)
            return;

        DataRow[] dr = dt.Select("Layer = " + intLayer.ToString() + " AND ParentId = '" + parentId + "'");

        ArrayList al = new ArrayList();
        ListItem li;

        string str = ""; int intCategoryLayerID = 0;
        foreach (DataRow d in dr)
        {
            str = "　| ";
            intCategoryLayerID = int.Parse(d["Layer"].ToString());
            if (intLayer == 1)
                str = " + ";
            for (int i = 1; i < intLayer * intLayer; i++)
                str += "--";

            li = new ListItem("\t" + str + "\t" + d["ProductName"].ToString(), d["ProductNumber"].ToString());
            dropCategory.Items.Add(li);
            DropdownlistDataBind(intCategoryLayerID + 1, d["ProductId"].ToString().Trim(), dt);
        }
    }
    #endregion

    #region 信息列表
    /// <summary>
    /// 信息列表
    /// </summary>
    void LoadingData()
    {
        try
        {
            StringBuilder SQL = new StringBuilder();
            SQL.Append(" AND DeletionStateCode = 0");
            if (Utils.IsNumber(RequestBase.GetFormString("productid")))
            {
                SQL.Append(" AND ProductNumber LIKE '" + RequestBase.GetFormString("productid") + "%'");
            }
            if (RequestBase.GetFormString("keys") != "")
            {
                SQL.Append(" AND (OrderNumber = '" + RequestBase.GetFormString("keys") + "' OR CardNo = '" + RequestBase.GetFormString("keys") + "' OR CardNo = '" + RequestBase.GetFormString("keys") + "' OR Name = '" + RequestBase.GetFormString("keys") + "' OR IDNo = '" + RequestBase.GetFormString("keys") + "' OR TName = '" + RequestBase.GetFormString("keys") + "' OR TIDNo = '" + RequestBase.GetFormString("keys") + "')");
            }
            if (Utils.IsNumber(RequestBase.GetFormString("status")))
            {
                SQL.Append(" AND StatusCode = " + RequestBase.GetFormString("status"));
            } 
            
            Control uc_rep = LoadControl("~/website_admin/insurance/_controls/ctlOrderList.ascx");
            Repeater rptOrderList = (Repeater)uc_rep.FindControl("rptOrderList");
            DBUtility.Pagination pagination = new DBUtility.Pagination();

            if (!string.IsNullOrEmpty(RequestBase.GetString("index")))
                pagination.Sequence = Convert.ToInt32(RequestBase.GetString("index"));
            else
                pagination.Sequence = 1;

            pagination.ItemUnit = "条记录";
            pagination.SortSentence = " ORDER BY OrderNumber DESC";
            pagination.Primarykey = "InsurancePolicyId";
            pagination.SearchField = "*";
            pagination.TableName = "vw_InsurancePolicyBaseForList";
            if (!string.IsNullOrEmpty(RequestBase.GetString("pagesize")))
                pagination.PageSize = Convert.ToInt32(RequestBase.GetString("pagesize"));
            else
                pagination.PageSize = 20;
            pagination.UrlStr = "";
            pagination.ImagesPath = "../_imgs/grid/";
            pagination.SearchSentence = SQL.ToString();

            rptOrderList.DataSource = pagination.GetDataTable();
            rptOrderList.DataBind();

            pagination.PageBarFlexigrid();

            uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, pagination.PageBar, null);
             
            string total = BLL.CommonBLL.ExecuteScalar("vw_InsurancePolicyBase", "ISNULL(SUM(TotalAmount), 0)", SQL.ToString()).ToString(); 
            uc_rep.GetType().GetProperty("TotalInfo").SetValue(uc_rep, total + "&nbsp;元", null); 

            StringBuilder strb = new StringBuilder();
            StringWriter sw = new StringWriter(strb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            rptOrderList.RenderControl(htw);

            BLL.AjaxHelper.ResponseWrite(strb.ToString());
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }
    #endregion

    #region 删除
    /// <summary>
    /// 删除目录
    /// </summary>
    void Delete()
    {
        /// 逻辑删除商品记录
        string res = "";
 
        if (Validate("sales online", 6))
            foreach (string _id in RequestBase.GetFormString("itemsid").Split(','))
            {
                res = BLL.Insurance.InsurancePolicyBase.Delete(_id, true);
                if (res != "succeeded")
                    break;
            }
        else
            res = "当前用户无操作权限！";

        BLL.AjaxHelper.ResponseWrite(res);
    }
    #endregion
}
